<?php

//------------------------------------------------------------------
// File: Event.class.php
// Author: Bryan Frost
// Date Created: 2010-03-25
//
// Event Management
//------------------------------------------------------------------


class EventPreviewData
{
	private $eventId;
	private $tournamentId;
	private $tournamentStartDate;
	private $divisionId;
	private $division;
	private $ratingId;
	private $ratingName;
	private $genderId;
	private $numberOfTeams;
	private $winningTeamId;
	
	
	
	
	
	private $startDate;
	private $endDate;
	private $genderName;
	private $teamSize;
	
	protected function setStartDate($startDate)						{ $this->startDate = $startDate; }
	public function GetStartDate()									{ return $this->startDate; }
	protected function setEndDate($endDate)							{ $this->endDate = $endDate; }
	public function GetEndDate()									{ return $this->endDate; }
	protected function setGenderName($genderName)					{ $this->genderName = $genderName; }
	public function GetGenderName()									{ return $this->genderName; }
	protected function setTeamSize($teamSize)						{ $this->teamSize = $teamSize; }
	public function GetTeamSize()									{ return $this->teamSize; }
	
	
	
	
	protected function setEventId($eventId)							{ $this->eventId = $eventId; }
	public function GetEventId()									{ return $this->eventId; }

	protected function setTournamentId($tournamentId)				{ $this->tournamentId = $tournamentId; }
	public function GetTournamentId()								{ return $this->tournamentId; }
	
	protected function setTournamentStartDate($tournamentStartDate)	{ $this->tournamentStartDate = $tournamentStartDate; }
	public function GetTournamentStartDate()						{ return $this->tournamentStartDate; }
	
	protected function setDivisionId($divisionId)					{ $this->divisionId = $divisionId; }
	public function GetDivisionId()									{ return $this->divisionId; }
	
	protected function setDivision($division)						{ $this->division = $division; }
	public function GetDivision()								{ return $this->division; }
	
	protected function setGenderId($genderId)						{ $this->genderId = $genderId; }
	public function GetGenderId()									{ return $this->genderId; }
	
	protected function setNumberOfTeams($numberOfTeams)				{ $this->numberOfTeams = $numberOfTeams; }
	public function GetNumberOfTeams()								{ return $this->numberOfTeams; }
		
	protected function setWinningTeamId($winningTeamId)				{ $this->winningTeamId = $winningTeamId; }
	public function GetWinningTeamId()								{ return $this->winningTeamId; }
	
	public function GetWinningTeam()
	{
		$team = Team::GetTeam($this->GetWinningTeamId());
		return $team;
	}
	
	public function GetEventFormat()
	{
		return sprintf('%s %s', GetTournamentGenderName($this->GetGenderId()), $this->GetDivision());
	}
}



class EventData extends EventPreviewData
{
	private $teamsCollection;
	
	protected function setTeamsCollection($teamsCollection)		{ $this->teamsCollection = $teamsCollection; }
	public function GetTeamsCollection()						{ return $this->teamsCollection; }
}



class Event extends EventData
{
	public function GetAllEventPreviews()
	{
		$eventPreviewCollection = array();
		
		$query  = "SELECT * FROM Events E ";
		$query .= "INNER JOIN Tournaments T USING (TournamentID)";
		$result = GetQueryResult($query);

		while($row = mysqli_fetch_array($result))
		{
			$eventPreviewCollection[] = Event::GetEventPreview($row['EventID']);
		}
		
		return $eventPreviewCollection;
	}
	
	public function GetCurrentSeasonEventPreviews()
	{
		$eventPreviewCollection = array();
		
		$query  = "SELECT * FROM Events E ";
		$query .= "INNER JOIN Tournaments T USING (TournamentID)";
		$result = GetQueryResult($query);

		while($row = mysqli_fetch_array($result))
		{
			$eventPreviewCollection[] = Event::GetEventPreview($row['EventID']);
		}
		
		return $eventPreviewCollection;
	}
	
	
	public static function GetEventPreview($eventId)
	{
		$e = new EventPreviewData();
		
		$query  = "SELECT *, (SELECT TeamID FROM Results WHERE EventID = $eventId AND Result = 1) AS Winner "; 
		$query .= "FROM Events E ";
		$query .= "INNER JOIN Results R USING (EventID) ";
		$query .= "INNER JOIN Tournaments T USING (TournamentID) ";
		$query .= "INNER JOIN Divisions D USING (DivisionID) ";
		$query .= "WHERE E.EventID = $eventId" ;
		$result = GetQueryResult($query);
		
		while($row = mysqli_fetch_array($result))
		{
			$e->setEventId($row['EventID']);
			$e->setTournamentId($row['TournamentID']);
			$e->setTournamentStartDate($row['StartDate']);
			$e->setDivisionId($row['RatingID']);
			$e->setDivision($row['Division']);
			$e->setGenderId($row['GenderID']);
			$e->setNumberOfTeams($row['NumberOfTeams']);
			$e->setWinningTeamId($row['Winner']);
			
		}	
		
		return $e;
	}
	
	
	// TEMPORARY WORK AROUND TO GET SCHEDULE DATA
	public static function GetEventPreviewForSchedule($eventId)
	{
		$e = new EventPreviewData();
		
		$query  = "SELECT * "; 
		$query .= "FROM Events E ";
		//$query .= "INNER JOIN Results R USING (EventID) ";
		$query .= "INNER JOIN Tournaments T USING (TournamentID) ";
		$query .= "INNER JOIN Divisions D USING (DivisionID) ";
		$query .= "WHERE E.EventID = $eventId" ;
		$result = GetQueryResult($query);
		
		while($row = mysqli_fetch_array($result))
		{
			$e->setEventId($row['EventID']);
			$e->setTournamentId($row['TournamentID']);
			$e->setTournamentStartDate($row['StartDate']);
			$e->setDivisionId($row['RatingID']);
			$e->setDivision($row['Division']);
			$e->setGenderId($row['GenderID']);
			//$e->setNumberOfTeams($row['NumberOfTeams']);
			//$e->setWinningTeamId($row['Winner']);
			
		}	
		
		return $e;
	}
	
	
	
	public static function GetEventData($eventDataId)
	{
		$ed = new EventData();
		
		$query  = "SELECT * ,(SELECT TeamID FROM Results WHERE EventID = $eventDataId AND Result = 1) AS Winner "; 
		$query .= "FROM Events E ";
		$query .= "INNER JOIN Results R USING (EventID) ";
		$query .= "INNER JOIN Tournaments T USING (TournamentID) ";
		$query .= "INNER JOIN Divisions D USING (DivisionID) ";
		$query .= "WHERE E.EventID = $eventDataId ";
		$query .= "ORDER BY Result ASC";
		$result = GetQueryResult($query);
		
		while($row = mysqli_fetch_array($result))
		{
			$ed->setEventId($row['EventID']);
			$ed->setTournamentId($row['TournamentID']);
			$ed->setTournamentStartDate($row['StartDate']);
			$ed->setDivisionId($row['DivisionID']);
			$ed->setDivision($row['Division']);
			$ed->setGenderId($row['GenderID']);
			$ed->setNumberOfTeams($row['NumberOfTeams']);
			$ed->setTeamsCollection(Event::GetEventTeams($ed->GetEventId()));
		}
		
		return $ed;
	}
	
	
	private function GetEventTeams($eventId)
	{
		$teamsCollection = array();
		
		$query  = "SELECT TeamID, Result FROM Results ";
		$query .= "WHERE EventID = $eventId ";
        $query .= "ORDER BY Result ASC";
		$result = GetQueryResult($query);
		
		$teamsCollection = array();
		
		while($row = mysqli_fetch_array($result))
		{
			$team = Team::GetTeam($row['TeamID']);
			
			$team = Team::AddResultToTeam($team, $row['Result']);
			
			$teamsCollection[] = $team;
		}
		
		return $teamsCollection;
	}
	
}




class PlayerEvent
{
	var	$eventDate;
	var $finish;
	var $points;
	var $numberOfTeams;
	
	// Required for Complete Events
	var $tournamentId;
	var $eventId;
	var $division;
	var $partner;
	var $genderId;
	var $gender;
	
	var $ratingEarned;
	
	var $divisionId;
	
	public static function GetPlayerEvent($eventDate, $finish, $points, $numberOfTeams)
	{
		$event = new PlayerEvent();

		$event->eventDate = $eventDate;
		$event->finish = $finish;
		$event->points = $points;
		$event->numberOfTeams = $numberOfTeams;

		return $event;
	}
	
//	public static function GetPlayerEventComplete($tournamentId, $eventId, $eventDate, $division, $partner, $finish, $points, $numberOfTeams, $genderId)
//	{
//		$event = new PlayerEvent();
//
//		$event->eventDate = $eventDate;
//		$event->finish = $finish;
//		$event->points = $points;
//		$event->numberOfTeams = $numberOfTeams;
//		$event->tournamentId = $tournamentId;
//		$event->eventId = $eventId;
//		$event->division = $division;
//		$event->partner = $partner;
//		$event->genderId = $genderId;
//		$event->genderName = GetTournamentGenderName($genderId);
//		
//		return $event;
//	}
	public static function GetPlayerEventComplete($tournamentId, $eventId, $eventDate, $divisionId, $partner, $finish, $points, $numberOfTeams, $genderId, $ratingEarned)
	{
		$event = new PlayerEvent();

		$event->eventDate = $eventDate;
		$event->finish = $finish;
		$event->points = $points;
		$event->numberOfTeams = $numberOfTeams;
		$event->tournamentId = $tournamentId;
		$event->eventId = $eventId;
		
		
		$event->divisionId = $divisionId;
		$event->division = GetDivisionName($divisionId);
		
		
		
		$event->partner = $partner;
		$event->genderId = $genderId;
		$event->gender = GetTournamentGenderName($genderId);
		
		$event->ratingEarned = $ratingEarned;
		
		return $event;
	}
}
	
?>